<?php

class Zing_Cache_Helper_Profiler
{       
       private $_key_profiler_hits = array();
       private $_key_profiler_misses = array();
       public $_total_misses = 0;
       public $_total_hits = 0;
       public $_total_time = 0;
       
       function __construct()
       {
       		
       }	   

       public function getCacheProfiles()
       {
	       return array_merge($this->_key_profiler_hits,$this->_key_profiler_misses);
       }
       
       public function getTotalMissesCache()
       {
	       return $this->_total_misses;	
       }
       
       public function getTotalHitsCache()
       {
	       return $this->_total_hits;
       }
       
       public function getTotalEllapsedTime()
       {
	       return $this->_total_time;
       }
       
       public function getPercentMissesCache()
       {
	       if($total == 0) return 0;
	       $total = $this->_total_hits + $this->_total_misses;
	       $percent = $this->_total_misses / $total;
	       return $percent * 100; 	
       }
       
       public function getPercentHitsCache()
       {
	       if($total == 0) return 0;
	       $total = $this->_total_hits + $this->_total_misses;
	       $percent = $this->_total_hits / $total;
	       return $percent * 100; 	
       }       
       
       public function pushToCacheProfiler($key, $result, $starttime, $endtime)
       {
       	   $debug = Globals::isDebug();
       	   if($debug == true)
       	   {
       	   		$_cache_result = new ResultCache();
		       $_cache_result->key = $key;
		       $diff = ($endtime - $starttime);
		       $this->_total_time += $diff;
		       $_cache_result->ellapsedtime = $diff;		
		       if($result === FALSE)
		       {			
			       $this->_total_misses++;
			       $_cache_result->result = 0;
			       $this->_key_profiler_misses[] = $_cache_result;
		       }
		       else 
		       {
			       $this->_total_hits++;
			       $_cache_result->result = 1;
			       $this->_key_profiler_hits[] = $_cache_result;
		       }
       	   }	       
       }	
       
       public function getProfilerData($cache_name = '')
       {
       	
       		$print .= '<br><br><br><table border=1 cellspacing="5" cellpadding="5" style="border-collapse:collapse">'
					. "<tr><th colspan=4 bgcolor='#dddddd'>Caching Profiler for instance " . $cache_name . "</th></tr>"
					. "<tr><th width=50>No.</th><th>Key</th><th>Result</th><th>Time elapsed in milisecs</th>";
					
			
			$print .= "<tr><td colspan='4' align='left'><b>total hits : " . $this->getTotalHitsCache() . "</b></td></tr>";
			$print .= "<tr><td colspan='4' align='left'><b>total percent hits : " . $this->getPercentHitsCache(). " %</b></td></tr>";
			$print .= "<tr><td colspan='4' align='left'><b>total misses : " . $this->getTotalMissesCache() . "</tr>";
			$print .= "<tr><td colspan='4' align='left'><b>total percent misses : " . $this->getPercentMissesCache() . " %</b></td></tr>";
			$print .= "<tr><td colspan='4' align='left'><b>total ellapsed time by cache : " . number_format($this->getTotalEllapsedTime(),9) * 1000 . " milisecs</b></td></tr>";
			
			$cache_profile = $this->getCacheProfiles();
			for($i=0;$i<count($cache_profile);$i++)
			{
				$profiler = $cache_profile[$i];
				$print .= "<tr><td>" . ($i+1) . "</td><td>" . $profiler->key . "</td><td>" 
						. ($profiler->result == "1" ? "<font color='green'>hit cache</font>" : "<font color='red'>miss cache</font>") 
						. "</td><td>" . number_format($profiler->ellapsedtime, 9) * 1000 . "</td></tr>";				 
			}
			$print .= "</table>";
			return $print;
       }       
}


?>
